package com.wandian.setting.mvvm.modifynicename;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.setting.data.BaseAppApi;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description 关于我们ViewModel
 **/
public class ModifyNiceNameViewModel extends BaseViewModel{

    private BaseAppApi baseAppApi;
    public ModifyNiceNameViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }
    public void modifyName(String nickName){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("nickName",nickName);
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        String key = AESUtils.getInstance().encrypt(hashMap);
        //  String key2 = RC4Util.encry(JsonUtil.toJson(hashMap));
        Log.d("crc_aeskey",key);
        Log.d("crc_aeskey_dec",AESUtils.getInstance().decrypt(key));
        //   Log.d("crc_rc4key_dec",RC4Util.decry(key2));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.upNickname(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("upNickname",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            ToastUtil.show("修改成功");
                            finish();
//                            String json = AESUtils.getInstance().decrypt(bean.getData())+"";
//                            Log.d("upNickname",   json);
//                            if(!UIUtils.isEmpty(json) && json.length()>4){
//                                LoginDataBean loginBean = (LoginDataBean)JsonUtil.parseJsonToBean(json,LoginDataBean.class);
//                                if(!UIUtils.isEmpty(loginBean)){
//                                    AppContext.getInstance().getAppPreferencesHelper().setToken(loginBean.getToken());
//                                    AppContext.getInstance().getAppPreferencesHelper().setKeyValue("isLogin","1");
//                                    AppContext.getInstance().getAppPreferencesHelper().setKeyValue("user_id",loginBean.getId());
//                                    ARouter.getInstance().build(RouterConfig.ROUTER_MAIN).navigation();
//                                }else {
//                                    ToastUtil.show("账号异常");
//                                }
//                            }else {
//                                ToastUtil.show("账号异常");
//                            }
                        }else {
                            ToastUtil.show("数据异常");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        Log.d("upNickname",   JsonUtil.toJson(e));
                        if(!UIUtils.isEmpty(e.getMessage())){
                            ToastUtil.show(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
